package xyz.lne.researchgroupmanage.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.lne.researchgroupmanage.annotation.AnonymousGetMapping;
import xyz.lne.researchgroupmanage.annotation.AnonymousPostMapping;
import xyz.lne.researchgroupmanage.controller.request.ResearchIdsRequest;
import xyz.lne.researchgroupmanage.controller.response.KeywordResponse;
import xyz.lne.researchgroupmanage.service.KeywordService;
import xyz.lne.researchgroupmanage.service.dataTransferObject.KeyWordDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author team7
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/keyword")
public class KeywordController {

    @Autowired
    private KeywordService keywordService;

    @AnonymousGetMapping("/keywordLists")
    List<KeywordResponse> findKeyword(String keyword){
        if(keyword==null || keyword.trim().length()==0){
            return new ArrayList<>();
        }

        return keywordService.findKeyword(keyword).stream().map(KeywordResponse::new).collect(Collectors.toList());
    }

    @AnonymousGetMapping("/researchGroupKeywords")
    List<KeywordResponse> findKeywordByResearchGroupId(Integer researchGroupId){
        return keywordService.findKeywordByResearchGroupId(researchGroupId).stream().map(KeywordResponse::new).collect(Collectors.toList());
    }

    @AnonymousGetMapping("/researchKeywords")
    List<KeywordResponse> findKeywordByResearchId(@RequestParam Integer researchId){
        return keywordService.findKeywordByResearchId(researchId).stream().map(KeywordResponse::new).collect(Collectors.toList());
    }

    @AnonymousGetMapping("/defaultKeywords")
    List<KeywordResponse> getDefaultKeywordList(){
        return keywordService.getDefaultKeywordList().stream().map(KeywordResponse::new).collect(Collectors.toList());
    }

    @AnonymousPostMapping("/getKeywordsListByResearchIdList")
    List<List<String>> getKeywordsListByResearchIdList(@RequestBody ResearchIdsRequest researchIdsRequest){
        List<Integer> researchIdList = researchIdsRequest.getResearchIdList();
       if(researchIdList.isEmpty()){
           return new ArrayList<>();
       }

       List<List<String>> ans=new ArrayList<>();

       for(Integer i:researchIdList){
           List<KeyWordDTO> dtoList=keywordService.findKeywordByResearchId(i);
           if(dtoList.isEmpty()){
               ans.add(new ArrayList<>());
           }
           else{
               ans.add(dtoList.stream().map(KeyWordDTO::getWord).collect(Collectors.toList()));
           }
       }

      return ans;

    }

    @AnonymousGetMapping("/getKeywordsListByResearchGroupIdList")
    List<List<String>> getKeywordsListByResearchGroupIdList(@RequestBody List<Integer> researchGroupIdList){

        if(researchGroupIdList.isEmpty()){
            return new ArrayList<>();
        }
        List<List<String>> ans=new ArrayList<>();

        for(Integer i:researchGroupIdList){
            List<KeyWordDTO> dtoList=keywordService.findKeywordByResearchGroupId(i);
            if(dtoList.isEmpty()){
                ans.add(new ArrayList<>());
            }
            else{
                ans.add(dtoList.stream().map(KeyWordDTO::getWord).collect(Collectors.toList()));
            }
        }

        return ans;
    }


}

